﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC26BO
    /// </summary>
    public class VYC26BO:DBConnectionBO
    {
        public VYC26BO():base()
        {
            //
        }
        public List<ModelVYC26> GetAllList()
        {
            var list = new List<ModelVYC26>();
            const string query = "SELECT [Id],[IdPhanQuyen],[ChuyenMuc],[LienKet],[IdAdmin],[NgayCapNhat],[TrangThai] FROM [VYC-26]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC26();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.IdPhanQuyen = (dataTable.Rows[i][1] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][1]);
                model.ChuyenMuc = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.LienKet = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.IdAdmin = (dataTable.Rows[i][4] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][4]);
                model.NgayCapNhat = (dataTable.Rows[i][5] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][5];
                model.TrangThai = (dataTable.Rows[i][6] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][6]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC26 model)
        {
            var query = "INSERT INTO [VYC-26]([IdPhanQuyen],[ChuyenMuc],[LienKet],[IdAdmin],[NgayCapNhat],[TrangThai]) VALUES (" + model.IdPhanQuyen + ", N'" + model.ChuyenMuc + "', N'" + model.LienKet + "', N'"
                        + model.IdAdmin + "GETDATE(), " + model.TrangThai + ")";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC26 model)
        {
            var query = "UPDATE [VYC-26] SET " +
                        "[IdPhanQuyen] = " + model.IdPhanQuyen + ", " +
                        "[ChuyenMuc]= N'" + model.ChuyenMuc + "', " +
                        "[LienKet] = N'" + model.LienKet + "', " +
                        "[IdAdmin] = " + model.IdAdmin + ", " +                        
                        "[NgayCapNhat] = GETDATE(), [TrangThai] = " + model.TrangThai +
                        " WHERE Id =" + model.Id + "'";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC26 model)
        {
            var query = "DELETE FROM [VYC-26] " +
                        " WHERE Id = " + model.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC26> GetById(int id)
        {
            var list = new List<ModelVYC26>();
            var query = "SELECT [Id],[IdPhanQuyen],[ChuyenMuc],[LienKet],[IdAdmin],[NgayCapNhat],[TrangThai] FROM [VYC-26] WHERE Id=" + id;
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC26();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.IdPhanQuyen = (dataTable.Rows[i][1] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][1]);
                model.ChuyenMuc = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.LienKet = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.IdAdmin = (dataTable.Rows[i][4] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][4]);
                model.NgayCapNhat = (dataTable.Rows[i][5] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][5];
                model.TrangThai = (dataTable.Rows[i][6] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][6]);

                list.Add(model);
            }
            return list;
        }

    }
}